package com.shell.customer.esign.service.impl;

import com.shell.bee.base.utils.StringUtil;
import com.shell.bee.esign.po.EsCust;
import com.shell.bee.esign.service.EsCustService;
import com.shell.bee.esign.service.EsignAcctService;
import com.shell.customer.accreditation.vo.UserVo;
import com.shell.customer.base.vo.CusRecVo;
import com.shell.customer.esign.service.EsignAcctTxService;
import com.shell.customer.po.CusRec;
import com.timevale.esign.sdk.tech.bean.AccountProfile;
import com.timevale.esign.sdk.tech.bean.OrganizeBean;
import com.timevale.esign.sdk.tech.bean.PersonBean;
import com.timevale.esign.sdk.tech.bean.UpdateOrganizeBean;
import com.timevale.esign.sdk.tech.bean.UpdatePersonBean;
import com.timevale.esign.sdk.tech.bean.result.AddAccountResult;
import com.timevale.esign.sdk.tech.bean.result.GetAccountProfileResult;
import com.timevale.esign.sdk.tech.bean.result.Result;
import com.timevale.esign.sdk.tech.impl.constants.OrganRegType;
import com.timevale.esign.sdk.tech.impl.constants.UserType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class EsignAcctTxServiceImpl implements EsignAcctTxService {
	@Autowired
	private EsignAcctService esignAcctService;
	@Autowired
	private EsCustService esCustService;

	public AddAccountResult openCustAcct(CusRec cus, UserVo user) {
		EsCust esCust = (EsCust) this.esCustService.selectByKey(cus.getCusId());
		if ((esCust != null) && (!StringUtil.emptyAndNull(esCust.getAcctId()))) {
			AddAccountResult result = new AddAccountResult();
			result.setErrCode(0);
			result.setAccountId(esCust.getAcctId());
			return result;
		}
		OrganizeBean bean = new OrganizeBean();
		bean.setName(cus.getCusName());
		OrganRegType regType;
		String orgCode;
		if (StringUtil.emptyAndNull(cus.getOrgCode())) {
			orgCode = cus.getSocCrCode();
			regType = OrganRegType.MERGE;
		} else {
			orgCode = cus.getOrgCode();
			regType = OrganRegType.NORMAL;
		}
		bean.setRegType(regType);
		bean.setOrganCode(orgCode);
		bean.setUserType(1);

		bean.setAgentName(user.getUserName());
		bean.setAgentIdNo(user.getCertNo());
		AddAccountResult result = this.esignAcctService.addOrganize(bean);
		if (result.getErrCode() == 0) {
			if (esCust == null) {
				esCust = new EsCust();
				esCust.setCustId(cus.getCusId());
				esCust.setCustNm(cus.getCusName());
				esCust.setAcctId(result.getAccountId());
				this.esCustService.save(esCust);
			} else {
				esCust.setAcctId(result.getAccountId());
				this.esCustService.updateNotNull(esCust);
			}
		} else if (result.getErrCode() == 150016) {
			int idNoType;
			if (StringUtil.emptyAndNull(cus.getOrgCode())) {
				idNoType = 22;
			} else {
				idNoType = 21;
			}
			GetAccountProfileResult getResult = this.esignAcctService.getAccountInfoByIdNo(orgCode, idNoType);
			if (getResult.getErrCode() == 0) {
				AccountProfile accountProfile = getResult.getAccountInfo();
				esCust = new EsCust();
				esCust.setCustId(cus.getCusId());
				esCust.setCustNm(cus.getCusName());
				esCust.setAcctId(accountProfile.getAccountUid());
				this.esCustService.saveOrUpdate(esCust);
				result.setErrCode(0);
				result.setAccountId(accountProfile.getAccountUid());
			}
		}
		return result;
	}

	public AddAccountResult openPersonAcct(UserVo user) {
		EsCust esCust = (EsCust) this.esCustService.selectByKey(user.getUserId());
		if ((esCust != null) && (!StringUtil.emptyAndNull(esCust.getAcctId()))) {
			AddAccountResult result = new AddAccountResult();
			result.setErrCode(0);
			result.setAccountId(esCust.getAcctId());
			return result;
		}
		PersonBean bean = new PersonBean();
		bean.setName(user.getUserName());
		bean.setMobile(user.getPhoneNo());
		bean.setIdNo(user.getCertNo());
		bean.setEmail(user.getEmail());
		AddAccountResult result = this.esignAcctService.addPerson(bean);
		if (result.getErrCode() == 0) {
			if (esCust == null) {
				esCust = new EsCust();
				esCust.setCustId(user.getUserId());
				esCust.setCustNm(user.getUserName());
				esCust.setMobile(user.getPhoneNo());
				esCust.setAcctId(result.getAccountId());
				this.esCustService.save(esCust);
			} else {
				esCust.setAcctId(result.getAccountId());
				this.esCustService.updateNotNull(esCust);
			}
		} else if (result.getErrCode() == 150016) {
			int idNoType = 11;
			GetAccountProfileResult getResult = this.esignAcctService.getAccountInfoByIdNo(user.getCertNo(), idNoType);
			if (getResult.getErrCode() == 0) {
				AccountProfile accountProfile = getResult.getAccountInfo();
				esCust = new EsCust();
				esCust.setCustId(user.getUserId());
				esCust.setCustNm(user.getUserName());
				esCust.setAcctId(accountProfile.getAccountUid());
				this.esCustService.saveOrUpdate(esCust);
				result.setErrCode(0);
				result.setAccountId(accountProfile.getAccountUid());
			}
		}
		return result;
	}

	public Result updateCustAcct(CusRecVo cus, UserVo user) {
		EsCust esCust = (EsCust) this.esCustService.selectByKey(cus.getCusId());
		if (esCust == null) {
			AddAccountResult ret = openCustAcct(cus, user);
			Result result = new Result();
			result.setErrCode(ret.getErrCode());
			result.setMsg(ret.getMsg());
			return result;
		}
		UpdateOrganizeBean bean = new UpdateOrganizeBean();
		bean.setName(cus.getCusName());
		bean.setUserType(UserType.AGENT);

		bean.setAgentName(user.getUserName());
		bean.setAgentIdNo(user.getCertNo());
		Result result = this.esignAcctService.updateOrganize(esCust.getAcctId(), bean, null);
		if (result.getErrCode() == 0) {
			esCust.setCustNm(cus.getCusName());
			this.esCustService.updateNotNull(esCust);
		}
		return result;
	}

	public Result updatePersonAcct(UserVo user) {
		EsCust esCust = (EsCust) this.esCustService.selectByKey(user.getUserId());
		if (esCust == null) {
			AddAccountResult ret = openPersonAcct(user);
			Result result = new Result();
			result.setErrCode(ret.getErrCode());
			result.setMsg(ret.getMsg());
			return result;
		}
		UpdatePersonBean bean = new UpdatePersonBean();
		bean.setName(user.getUserName());
		bean.setMobile(user.getPhoneNo());
		bean.setEmail(user.getEmail());
		Result result = this.esignAcctService.updatePerson(esCust.getAcctId(), bean, null);
		if (result.getErrCode() == 0) {
			esCust.setCustNm(user.getUserName());
			esCust.setMobile(user.getPhoneNo());
			this.esCustService.updateNotNull(esCust);
		}
		return result;
	}
}
